package com.ruoyi.accounting.controller;

import com.ruoyi.accounting.domain.AccAccountingRecord;
import com.ruoyi.accounting.domain.dto.AccAccountingRecordDTO;
import com.ruoyi.accounting.service.AccAccountingRecordService;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 流水Controller
 *
 * @author ruoyi
 * @date 2025-07-13
 */
@RestController
@RequestMapping("/record")
public class AccAccountingRecordController extends BaseController {
    @Autowired
    private AccAccountingRecordService accAccountingRecordService;

    /**
     * 查询流水列表
     */
    @RequiresPermissions("system:record:list")
    @GetMapping("/list")
    public TableDataInfo list(AccAccountingRecord accAccountingRecord) {
        startPage();
        List<AccAccountingRecord> list = accAccountingRecordService.selectAccAccountingRecordList(accAccountingRecord);
        return getDataTable(list);
    }

    /**
     * 导出流水列表
     */
    @RequiresPermissions("system:record:export")
    @Log(title = "流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccAccountingRecord accAccountingRecord) {
        List<AccAccountingRecord> list = accAccountingRecordService.selectAccAccountingRecordList(accAccountingRecord);
        ExcelUtil<AccAccountingRecord> util = new ExcelUtil<AccAccountingRecord>(AccAccountingRecord.class);
        util.exportExcel(response, list, "流水数据");
    }

    /**
     * 获取流水详细信息
     */
    @RequiresPermissions("system:record:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(accAccountingRecordService.selectAccAccountingRecordById(id));
    }

    /**
     * 新增流水
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "流水", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid AccAccountingRecordDTO accAccountingRecordDTO) {
        AccAccountingRecord accAccountingRecord = new AccAccountingRecord();
        BeanUtils.copyBeanProp(accAccountingRecord, accAccountingRecordDTO);
        return toAjax(accAccountingRecordService.insertAccAccountingRecord(accAccountingRecord));
    }

    /**
     * 修改流水
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "流水", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccAccountingRecord accAccountingRecord) {
        return toAjax(accAccountingRecordService.updateAccAccountingRecord(accAccountingRecord));
    }

    /**
     * 删除流水
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "流水", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(accAccountingRecordService.deleteAccAccountingRecordByIds(ids));
    }

    /**
     * 上传Excel并导入数据
     * @param file 上传的Excel文件
     * @return 导入的交易数据列表
     */
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file, Long bookId) throws Exception {
        return toAjax(accAccountingRecordService.importFromExcel(file,bookId));
    }
}
